/*
 * Created by BAC IT Deustche Bank
 * User: Sergio Vasquez
 * Date: 6/11/2009
 * Time: 4:21 PM
 * 
 * Description:
 * 
 */
using IScheduler.Core.Business;
using System;
using System.Collections.Generic;

namespace IScheduler.Core.Managers
{
	/// <summary>
	/// Description of ParserHolidayManager.
	/// </summary>
	public class ParserHolidayManager : IHolidayManager
	{
		/// <summary>
		/// 
		/// </summary>
		public ParserHolidayManager()
		{
			this.InitializeHolidayManager();
		}
		
		/// <summary>
		/// 
		/// </summary>
		public IList<Holiday> HolidaysList { get; set; }
		
		/// <summary>
		/// 
		/// </summary>
		public void InitializeHolidayManager()
		{
			try
			{
				HolidayBusiness holidayBusiness = new HolidayBusiness();
				this.HolidaysList = holidayBusiness.Holidays();
			}
			catch(ManagerException e)
			{
				throw e;
			}
		}

		/// <summary>
		/// Check if the passed day is holiday or not.
		/// </summary>
		/// <param name="day"></param>
		/// <returns></returns>
		private bool CheckHolidayList(DateTime day)
		{
			IEnumerator<Holiday> list = this.HolidaysList.GetEnumerator();
			
			while (list.MoveNext())
			{
				if (UtilService.CompareTwoDates(list.Current.Date, day))
				{
					return true;
				}
			}
			return false;			
		}
		
		/// <summary>
		/// Check if the passed date is holiday.
		/// </summary>
		/// <param name="day"></param>
		/// <returns></returns>
		public bool IsHoliday(DateTime day)
		{
			if (day == null)
			{
				//TODO: Add a message here
				throw new ArgumentException ("The parameter can not be null.", "day:Datetime");
			}
			
			if (this.HolidaysList == null)
			{
				//TODO: Add a message here
				throw new ManagerException("The holiday was not loaded.", this.ToString());
			}
			
			if (CheckHolidayList(day))
			{
				return true;
			}
			
			return false;
		}
		
		/// <summary>
		/// Check if the current day is holiday.
		/// </summary>
		/// <returns></returns>
		public bool IsTodayHoliday()
		{
			if (this.HolidaysList == null)
			{
				//TODO: Add a message here
				throw new ManagerException("The holiday was not loaded.", this.ToString());
			}
			return CheckHolidayList(DateTime.Now);
		}
	}
}
